import tkinter as tk#导入
from tkinter import *
import re
from bokeh.layouts import column

#李坤负责的计算机类,包含加减乘除 换算功能
from calculate import Calculator
from NumToUpper import setWindow
#袁羽负责的绘图窗口
from testf import plot_handler # 导入绘图窗口函数

#实例化object,建立窗口window
window = tk.Tk()
#给窗口的可视化取名
window.title('Calculator')
#设定窗口大小(长*宽)
window.geometry('360x699')

#计算器的显示屏
#Entry为文本输入域
#display = Entry(window,width=10, bd=4,font=("Arial", 24),justify='right',relief=SUNKEN)
#label标签可以显示文字
frame=tk.Frame(window,width=10, bd=4,relief=SUNKEN)
frame.grid(row=0,  column=0, columnspan=5, sticky='ew')
display = tk.Label(frame,width=10, bd=4,font=("Arial", 24),justify='right',text='0',anchor='e')
display0 = tk.Label(frame,width=10, bd=4,font=("Arial", 24),justify='right',anchor='e')
#方格放置
display0.grid(row=0,  column=0, columnspan=5, sticky='ew',ipady=30)
display.grid(row=1,  column=0, columnspan=5, sticky='ew', ipady=30)



scientific_computing_buttons=[
    '(',')','解方程','求导','换算',
    '\u221AX','X\u2074','X\u00B3','X\u02B8','e\u207F',
    'X!','sin','cos','tan','e',
    'Rand','sinh','cosh','tanh','\u03C0',
    '\u232B','\u207A/\u208B','%','\u00F7','mr',
    '7','8','9','x','10\u207F',
    '4','5','6','-','log\u2081\u2080',
    '1','2','3','+','EE',
    '绘图','0','.','=','Rad'
]
#2024/11/4 李坤修改下列代码,将按钮和函数绑定,新增函数列表
calculator=Calculator()
scientific_computing_methods=[
    lambda: button_on_click('('),lambda: button_on_click(')'),'解方程','求导',lambda: setWindow(window),
    '\u221AX','X\u2074','X\u00B3','X\u02B8','e\u207F',
    'X!','sin','cos','tan','e',
    'Rand','sinh','cosh','tanh','\u03C0',
    lambda: button_on_click('d'),'\u207A/\u208B','%',lambda: button_on_click('/'),'mr',
    lambda: button_on_click('7'),lambda: button_on_click('8'),lambda: button_on_click('9'),lambda: button_on_click('*'),'10\u207F',
    lambda: button_on_click('4'),lambda: button_on_click('5'),lambda: button_on_click('6'),lambda: button_on_click('-'),'log\u2081\u2080',
    lambda: button_on_click('1'),lambda: button_on_click('2'),lambda: button_on_click('3'),lambda: button_on_click('+'),'EE',
    lambda: plot_handler(window),lambda: button_on_click('0'),lambda: button_on_click('.'),lambda: button_on_click('='),'Rad'
]

s_row_value=1
s_col_value=0

def show():
    display0.config(text=calculator.showResult())
    display.config(text=calculator.showCurrent())
def button_on_click(str):
    #正则表达式匹配
    if re.match(r'^[0-9]$', str):
        calculator.addNumber(str)
    elif str == "." or str=='(' or str==')':
        calculator.addNumber(str)
    elif str=="=":
        calculator.eqv()
        display0.config(text='')
    elif str == "d":
        calculator.deleteone()
    else:
        calculator.addMethod(str)
    show()


for button, method in zip(scientific_computing_buttons, scientific_computing_methods):
    #callable内置函数判断是否为方法
    if callable(method):
        button=Button(window,text=button,width=7,command=method)
    else:
        button = Button(window, text=button, width=7)
    button.grid(row=s_row_value, column=s_col_value, padx=3, pady=3, ipadx=3, ipady=3)
    s_col_value+=1
    if s_col_value>4:
        s_row_value+=1
        s_col_value=0
#李坤修改结束

'''张坤吉原代码
s_row_value=1
s_col_value=0

for button in scientific_computing_buttons:
    button=Button(window,text=button,width=7)
    button.grid(row=s_row_value, column=s_col_value, padx=3, pady=3, ipadx=3, ipady=3)
    s_col_value+=1
    if s_col_value>4:
        s_row_value+=1
        s_col_value=0
'''
for i in range(5):
    window.columnconfigure(i, weight=1)  # 每列权重都是1，因此它们会均匀伸展

for i in range(10):
    window.rowconfigure(i, weight=1)  # 每行权重都是1，因此它们也会均匀伸展

# n_row_value=7
# n_col_value=0
# for button1 in normal_computing_buttons:
#     button1=Button(window,text=button1,width=7)
#     button1.grid(row=n_row_value,column=n_col_value,padx=3, pady=3, ipadx=3, ipady=3)
#     n_col_value+=1
#     if n_col_value>4:
#         n_row_value+=1
#         n_col_value=0



window.mainloop()#循环,使得每次点击都有反馈
